<template>
	<view class="addgroup">
		<u-navbar left-icon-color="#fff" :placeholder="true" :bgColor="bgColor" :autoBack="true">
			<view class="navtitle" slot="center">
				设置禁言
			</view>
		</u-navbar>
		<view class="search">
			<u--input placeholder="请输入对方账号或备注" customStyle="background-color: #232526; padding: 20rpx;"
				prefixIconStyle="color: #fff;" @confirm="gosearch" @input="input"  prefixIcon="search" color="#fff" border="none"
				v-model="value"></u--input>
		</view>
		<view class="tabs">
			<u-tabs :list="tabslist" :current="current" lineColor="#fff" activeStyle="color:#fff" inactiveStyle="color:#d5d5d5"
				@click="click"></u-tabs>
		</view>
		<view class="index-list">
			<view v-if="list.length > 0">
				<checkbox-group @change="checkboxChange">
					<label class="uni-list-cell uni-list-cell-pd" v-for="(item,index) in list" :key="item.value">
						<view class="anchor-list">
							<view class="anchor-left">
								<!-- 使用 u-checkbox 组件作为复选框 -->
								<checkbox :value="current == 0? JSON.stringify(item.id):item.userId " color="#17191A" :checked="item.show" />
							</view>
							<view class="anchor-right">
								<!-- 在 u-image 中展示用户头像 -->
								<u-image :showLoading="true" radius="10" :src="item.u_img"
									loadingIcon="/static/chat/logo.png" errorIcon="/static/chat/logo.png" width="50px"
									height="50px">
								</u-image>
								<!-- 根据条件显示用户名或备注 -->
								<view class="text">{{ item.user }}</view>
							</view>
						</view>
					</label>
				</checkbox-group>
			</view>
			<view v-if="list.length < 1">
				<u-empty mode="list" text="成员列表为空" :marginTop="100" icon="http://cdn.uviewui.com/uview/empty/list.png">
				</u-empty>
			</view>
		</view>
		<view style="height: 100rpx;"></view>
		<view class="flex-btn">
			<view class="left-box">
				<u-button type="primary" v-if="checkall == false" @click="goall" style="color: #17191A;"
					color="linear-gradient(90deg, #C8F673 0%, #98F083 100%)" text="全选"></u-button>
				<u-button type="primary" v-else @click="gocellall" style="color: #17191A;"
					color="linear-gradient(90deg, #C8F673 0%, #98F083 100%)" text="取消全选"></u-button>
			</view>
			<view class="right-box">
				<u-button type="error" v-if="current == 0" @click="gospeech(1)" text="禁言"></u-button>
				<u-button type="primary" v-if="current == 1" @click="gospeech(2)" text="解除禁言"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				bgColor: 'rgba(0,0,0,0.0)',
				value: '',
				list: [],
				selectedItems: [],
				checkall: false,
				current:0,
				tabslist: [{
						name: '成员列表'
					},
					{
						name: '已禁言'
					}
				]
			}
		},
		onLoad(e) {
			this.id = e.id
			this.userlist()
		},
		methods: {
			gosearch(){
				if(this.value == ''){
					uni.showToast({
						title:'请输入对方账号！',
						icon:'none'
					})
					return
				}
				let searchlist = this.list.flat().filter(item =>
					item.user.includes(this.value)
				);
				this.list = searchlist
			},
			input(e) {
				if(e == ''){
					if(this.current == 0){
						this.userlist()
					}else{
						this.userspeechlist()
					}
				}
			},
			click(e){
				this.value = ''
				this.checkall = false
				this.current = e.index
				this.list = []
				if(e.index == 0){
					this.userlist()
				}else{
					this.userspeechlist()
				}
			},
			goall() {
				if(this.list.length < 1){
					uni.showToast({
						title:'成员为空！',
						icon:'none'
					})
					return
				}
				this.list.map(item => {
					item.show = true
					this.selectedItems.push(item.id)
				})
				this.checkall = true
			},
			gocellall() {
				if(this.list.length < 1){
					uni.showToast({
						title:'成员为空！',
						icon:'none'
					})
					return
				}
				this.list.map(item => {
					item.show = false
					this.selectedItems = []
				})
				this.checkall = false
			},
			gospeech(type) {
				console.log(this.selectedItems)
				if (this.selectedItems.length < 1) {
					uni.showToast({
						title: '请选择成员！',
						icon: 'none'
					})
					return
				}
				this.$Request({
					method: 'POST',
					url: '/api/rong/getGroupGag',
					data: {
						group_id: this.id,
						member_ids:this.selectedItems.toString(),
						type:type
					}
				}).then(res => {
					if (res.code == 1) {
						if(type == 1){
							uni.showToast({
								title:'成员已被禁言！',
								icon:'none'
							})
						}else{
							uni.showToast({
								title:'成员已解除禁言！',
								icon:'none'
							})
						}
						if(this.current == 0){
							this.userlist()
						}else{
							this.userspeechlist()
						}
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			checkboxChange(e) {
				this.selectedItems = e.detail.value
				console.log(this.selectedItems)
			},
			gochat(item) {
				let name = item.break
				if (item.break == '') {
					name = item.user
				}
				uni.navigateTo({
					url: '/pages/chat/chitchat/index?id=' + item.member_id + '&name=' + name
				})
			},
			gourl(url) {
				uni.navigateTo({
					url: url
				})
			},
			userspeechlist(){
				this.$Request({
					method: 'POST',
					url: '/api/rong/seeGroupGagInfo',
					data: {
						group_id: this.id
					}
				}).then(res => {
					if (res.code == 1) {
						let list = res.data.members
						if(list.length < 1){
							this.list = []
						}else{
							let arr = []
							list.map(item => {
								if (item.is_hman != 1) {
									item.show = false
									arr.push(item)
								}
							})
							this.list = arr
						}
						
					}
				})
			},
			userlist() {
				this.$Request({
					method: 'POST',
					url: '/api/rong/getGroupInfo',
					data: {
						group_id: this.id
					}
				}).then(res => {
					if (res.code == 1) {
						let list = res.data
						if(list.length < 1){
							this.list =[]
						}else{
							let arr = []
							list.map(item => {
								if (item.is_hman != 1) {
									item.show = false
									arr.push(item)
								}
							})
							this.list = arr
						}
					}
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #17191A;
	}
</style>
<style scoped lang="scss">
	.addgroup {
		.search{
			padding: 30rpx 30rpx 20rpx 30rpx;
		}
		.tabs{
			width: 300rpx;
			margin: 0 auto;
			display: flex;
			justify-content: center;
		}
		.flex-btn {
			width: 100%;
			padding: 30rpx;
			position: fixed;
			bottom: 0;
			left: 0;
			background-color: #1D2123;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.left-box{
				width: 30%;
			}
			.right-box{
				width: 60%;
			}
		}

		.navtitle {
			color: #fff;
		}

		.index-list {
			padding: 30rpx;

			.anchor-list {
				width: 100vw;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 30rpx;

				.anchor-left {
					width: 8%;
				}

				.anchor-right {
					width: 90%;
					display: flex;
					align-items: center;

					.text {
						color: #fff;
						font-size: 28rpx;
						margin-left: 20rpx;
					}
				}
			}
		}
	}
</style>